﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace  Utility
{
    public class AppConfig
    {
        public static string MainConStr = ConfigurationManager.ConnectionStrings["MainConnectionString"].ToString();
        public static string CDBConStr = ConfigurationManager.ConnectionStrings["CommunityConnString"].ToString();

        public static readonly string HttpListenerApp = GetStringConfigValue("HttpListenerApp"); //HttpListener监听地址

        public static string LocatIP = GetStringConfigValue("LocatIP"); //本机IP地址

        public static string PostWebRequestIP = GetStringConfigValue("PostWebRequestIP"); //post请求地址

        //public static string HttpIP = ConfigurationManager.AppSettings["HttpIP"].ToString();
        //public static string ToldConStr = ConfigurationManager.ConnectionStrings["ToldConnString"].ToString();

        
        #region 格式化各类型 Config
        /// <summary>
        /// 获取字符串型Config值
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        private static string GetStringConfigValue(string keyName)
        {
            try
            {
                return ConfigurationManager.AppSettings[keyName].ToString();
            }
            catch
            {
                //Log4Net.LogError("GlobalData/GetStringConfigValue()", keyName + "配置项有误");
                LogUtility.Error("AppConfig/GetStringConfigValue()", keyName + "配置项有误");
                return "";
            }
        }

        /// <summary>
        /// 获取bool型Config值
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        private static bool GetBoolConfigValue(string keyName)
        {
            try
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings[keyName]);
            }
            catch
            {
                //Log4Net.LogError("GlobalData/GetBoolConfigValue()", keyName + "配置项有误");
                LogUtility.Error("AppConfig/GetBoolConfigValue()", keyName + "配置项有误");
                return false;
            }
        }

        /// <summary>
        /// 获取数字型Config值
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        private static Int32 GetInt32ConfigValue(string keyName)
        {
            Regex regex = new Regex("^\\d*$");
            string tempStr;
            try
            {
                tempStr = ConfigurationManager.AppSettings[keyName].ToString();
                if (regex.IsMatch(tempStr))
                {
                    return Convert.ToInt32(tempStr);
                }

                return 0;
            }
            catch
            {
                //Log4Net.LogError("GlobalData/GetInt32ConfigValue()", keyName + "配置项有误");
                LogUtility.Error("AppConfig/GetInt32ConfigValue()", keyName + "配置项有误");
                return 0;
            }
        }
        #endregion
    }
}
